
let num = 1
const res = []
const fileName = 'zhongan'
const cate = [0, 2002, 46001]
let index = 0
function fetchData() {
  console.log(num);
  fetch('https://gwbk.zhongan.com/appapi/za-dm-news/pc/getArticleList', {
    method: 'POST',
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify({ "directoryId": cate[index], "pageNum": num, "pageSize": 10 })
  })
    .then(r => r.json())
    .then(r => {
      res.push(...r.result.artPage.list)
      num++
      if (num < 10) {
        const t = setTimeout(() => {
          clearTimeout(t)
          fetchData()
        }, 1000)
      }
      else if (index < cate.length) {
        num = 1
        index++
        if (index === cate.length) {
          console.log('end----');
          downloadText(`${fileName}.json`, JSON.stringify(res))
        }
        else fetchData()
      }

    })

}

fetchData()

function downloadText(fileName, text) {
  const url = window.URL || window.webkitURL || window;
  const blob = new Blob([text]);
  const saveLink = document.createElement("a");
  saveLink.href = url.createObjectURL(blob);
  // 设置 download 属性
  saveLink.download = fileName + '.json';;
  saveLink.click();
  url.revokeObjectURL(saveLink.href);
}
